﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TriggerCollisionAdv : TriggerBase
{
    public LayerMask triggerLayer;
    public ETriggerDirection triggerDirection;
    float heightWidth;
    float widthHeight;

    public override void InitTrigger()
    {
        base.InitTrigger();
        heightWidth = GetComponent<BoxCollider2D>().bounds.size.y / GetComponent<BoxCollider2D>().bounds.size.x;
        widthHeight = GetComponent<BoxCollider2D>().bounds.size.x / GetComponent<BoxCollider2D>().bounds.size.y;
    }

    void OnTriggerEnter2D(Collider2D collider2d)
    {

        Debug.LogError("collider2d " + collider2d.gameObject.name);
        if (Util.IsInLayerMask(collider2d.gameObject.layer, triggerLayer))
        {
            Vector3 heading = collider2d.transform.position - transform.position;
            float distance = heading.magnitude;
            Vector3 direction = heading / distance;
            ETriggerDirection curDir = ETriggerDirection.None;

            if (Mathf.Abs(direction.x / widthHeight) > Mathf.Abs(direction.y / heightWidth))
            {
                if (Mathf.Sign(direction.x) == 1)
                {
                    print("Hit From Right");
                    curDir = ETriggerDirection.Right;
                }

                if (Mathf.Sign(direction.x) == -1)
                {
                    print("Hit From Left");
                    curDir = ETriggerDirection.Left;
                }
            }
            if (Mathf.Abs(direction.x / widthHeight) < Mathf.Abs(direction.y / heightWidth))
            {
                if (Mathf.Sign(direction.y) == 1)
                {
                    print("Hit From Top");
                    curDir = ETriggerDirection.Top;
                }
                if (Mathf.Sign(direction.y) == -1)
                {
                    print("Hit From Bot");
                    curDir = ETriggerDirection.Bottom;
                }
            }
            if (curDir == triggerDirection)
                OnTrigger();
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.LogError("Collision2D " + collision.gameObject.name);
        if (Util.IsInLayerMask(collision.gameObject.layer, triggerLayer))
        {
            ETriggerDirection curDir = ETriggerDirection.None;
            if (collision.contacts[0].normal.y == -1)
            {
                Debug.Log("++++++++++++++从上方碰撞");
                curDir = ETriggerDirection.Top;
            }
            else if (collision.contacts[0].normal.y == 1)
            {
                Debug.Log("++++++++++++++从下方碰撞");
                curDir = ETriggerDirection.Bottom;
            }
            else if (collision.contacts[0].normal.x == -1)
            {
                Debug.Log("++++++++++++++从左边碰撞");
                curDir = ETriggerDirection.Left;
            }
            else if (collision.contacts[0].normal.x == 1)
            {
                Debug.Log("++++++++++++++从右边碰撞");
                curDir = ETriggerDirection.Right;
            }
            if (curDir == triggerDirection)
                OnTrigger();

        }
    }
}
